package net.tycmc.cty.ctyms.timejob.function.mail;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.DynamicReadConfig;

import org.apache.commons.mail.HtmlEmail;

/**
 * 邮件发送类
 * @author chengshichao
 * 创建时间：2013-04-13
 */
public class SendMail {

	//配置文件名称
	private final static String PROPFILENAME = "mail.config.properties";
	
	private final static Pattern emailer = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
	private SendMail(){}
	
	private static class sendMailHolder {  
		static SendMail sendMail = new SendMail();  
	} 
	
	public static SendMail getInstance(){
		return sendMailHolder.sendMail;
	}
	
	/**
	 * 发送邮件
	 * @param subject 主题
	 * @param msg 信息内容，支持HTML
	 * @param emailTo 接收人邮箱集合
	 * @return 1：发送成功	2：发送失败
	 */
	public int sendMail(String subject,String msg,List<String> emailTo){
		
		try{
			HtmlEmail email = this.getHtmlEmail(subject, msg);
			for(String mail:emailTo){
				if(emailer.matcher(mail).matches())
					email.addTo(mail);
			}
			//发送邮件
			String retInfo = email.send();
			int sendStatus;
			//暂时发送状态判断比较简单
			if(StringUtil.isNullOrEmpty(retInfo)){
				sendStatus = 1;
			}else{
				sendStatus = 2;
			}
			return sendStatus;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 2;
	}
	
	/**
	 * 发送邮件
	 * @param subject 主题
	 * @param msg 信息内容，支持HTML
	 * @param emailTo 接收人邮箱集合
	 * @param emailKey emailTo中Map（接收人邮箱信息）的KEY
	 * @return 1：发送成功	2：发送失败     3：无接收人
	 */
	public int sendMail(String subject,String msg,List<Map<String,Object>> emailTo,String emailKey){
		if(emailTo==null || emailTo.size()==0)return 3;
		try{
			HtmlEmail email = this.getHtmlEmail(subject, msg);
			for(Map<String,Object> mail:emailTo){
				if(emailer.matcher(MapGetter.getString(mail, emailKey)).matches())
					email.addTo(MapGetter.getString(mail, emailKey));
			}
			//发送邮件
			String retInfo = email.send();
			int sendStatus;
			//暂时发送状态判断比较简单
			if(StringUtil.isNullOrEmpty(retInfo)){
				sendStatus = 1;
			}else{
				sendStatus = 2;
			}
			return sendStatus;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 2;
	}
	
	/**
	 * 获取HtmlEmail对象
	 * @param subject
	 * @param msg
	 * @return
	 * @throws Exception
	 */
	private HtmlEmail getHtmlEmail(String subject,String msg) throws Exception {
		//邮件服务器配置信息，暂时每次都从配置文件中获取
		String hostName = DynamicReadConfig.getConfigProperty(PROPFILENAME, "HostName");
		String authenticateName = DynamicReadConfig.getConfigProperty(PROPFILENAME, "AuthenticateName");
		String authenticatePwd = DynamicReadConfig.getConfigProperty(PROPFILENAME, "AuthenticatePwd");
		String senderMail = DynamicReadConfig.getConfigProperty(PROPFILENAME, "SenderMail");
		String senderName = DynamicReadConfig.getConfigProperty(PROPFILENAME, "SenderName");
		String msgSuffix = DynamicReadConfig.getConfigProperty(PROPFILENAME, "MsgSuffix");
		String msgInscribe = DynamicReadConfig.getConfigProperty(PROPFILENAME, "MsgInscribe");
		
		HtmlEmail email = new HtmlEmail();
		email.setHostName(hostName);
		email.setSmtpPort(25);
		email.setAuthentication(authenticateName, authenticatePwd);
		email.setCharset("UTF-8");
		email.setFrom(senderMail, senderName);
		email.setSubject(subject);
		email.setMsg(msg+msgSuffix+"<br/><b>"+msgInscribe+"</b>");
		return email;
	}
	
	public static void main(String[] args) throws Exception{
		List<String> list = new ArrayList<String>();
		list.add("chengshichao@tycmc.net");
		System.out.println(SendMail.getInstance().sendMail("京123445—12日14：56－保养提醒", "京123445—12日14：56－保养提醒。系统自动发送，请勿回复！", list));
	}
}
